package tank1;

import java.io.*;
import java.util.Vector;

/**
 * Version: 1.1
 * Author: TangJun
 * Date: 2024-08-01
 */
public class Recorder {
    private static int destroyNum = 0;//击毁坦克数量
    private static Vector<Tank> tanks;//保存使用
    private static Vector<Node> nodes;//读取使用
    private static String fileRecord = "src\\dataRecord.txt";
    private static BufferedWriter bw;
    private static BufferedReader br;

    //加载文件
    public static void Load(){
        nodes = new Vector<Node>();
        try {
            br = new BufferedReader(new FileReader(fileRecord));
            destroyNum = Integer.parseInt(br.readLine());
            String data;
            while ((data = br.readLine()) != null) {
                String[] str = data.split(" ");
                Node node = new Node(Integer.parseInt(str[0]),
                        Integer.parseInt(str[1]),
                        Integer.parseInt(str[2]));
                nodes.add(node);
            }
            System.out.println("加载完成");
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
    //保存文件
    public static void Save() {
        try {
            bw = new BufferedWriter(new FileWriter(fileRecord));
            bw.write(destroyNum + "\r\n");
            for (Tank tank : tanks) {
                if (tank.isLive()) {
                    String tankData = tank.getX() + " " + tank.getY() + " " + tank.getDirect() + "\r\n";
                    bw.write(tankData);
                }
            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    public static String getFileRecord() {
        return fileRecord;
    }

    public static Vector<Node> getNodes() {
        return nodes;
    }

    public static void setTanks(Vector<Tank> tanks) {
        Recorder.tanks = tanks;
    }

    public static void DestroyTank() {
        destroyNum++;
    }
    public static int getDestroyNum() {
        return destroyNum;
    }
}
